package com.atguigu.srb.core.service.impl;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author tjf
 * @since 2022-01-03
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public String commitBindUser(UserBindVO userBindVO, Long userId) {
        // 判断身份证未绑定
        LambdaQueryWrapper<UserBind> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBind::getIdCard, userBindVO.getIdCard())
                .ne(UserBind::getUserId, userId);
        UserBind userBind = baseMapper.selectOne(queryWrapper);
        Assert.isNull(userBind, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);

        // 用户是否曾经填写过绑定表单
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBind::getUserId, userId);
        userBind = baseMapper.selectOne(queryWrapper);

        // 数据库中创建或更新用户绑定记录
        if (userBind == null) {
            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVO, userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            baseMapper.insert(userBind);
        } else {
            BeanUtils.copyProperties(userBindVO, userBind);
            baseMapper.updateById(userBind);
        }

        // 组装自动提交表单的参数
        return FormHelper.buildForm(HfbConst.USERBIND_URL, new HashMap<String, Object>() {{
            put("agentId", HfbConst.AGENT_ID);
            put("agentUserId", userId);
            put("idCard", userBindVO.getIdCard());
            put("personalName", userBindVO.getName());
            put("bankType", userBindVO.getBankType());
            put("bankNo", userBindVO.getBankNo());
            put("mobile", userBindVO.getMobile());
            put("returnUrl", HfbConst.USERBIND_RETURN_URL);
            put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
            put("timestamp", RequestHelper.getTimestamp());
            put("sign", RequestHelper.getSign(this));
        }});
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void notify(Map<String, Object> map) {

        String userId = (String) map.get("agentUserId");
        String bindCode = (String) map.get("bindCode");

        // 更新用户绑定表
        LambdaQueryWrapper<UserBind> userBindQueryWrapper = new LambdaQueryWrapper<>();
        userBindQueryWrapper.eq(UserBind::getUserId, userId);
        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);
        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        baseMapper.updateById(userBind);

        // 更新用户表
        UserInfo userInfo = userInfoMapper.selectById(userId);
        userInfo.setBindCode(bindCode);
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public String getBindCodeByUserId(Long userId) {
        LambdaQueryWrapper<UserBind> userBindLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userBindLambdaQueryWrapper.select(UserBind::getBindCode).eq(UserBind::getUserId, userId);
        UserBind userBind = baseMapper.selectOne(userBindLambdaQueryWrapper);
        return userBind.getBindCode();
    }

    @Override
    public String getMobileByBindCode(String bindCode) {
        LambdaQueryWrapper<UserBind> userBindLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userBindLambdaQueryWrapper.eq(UserBind::getBindCode, bindCode);
        return baseMapper.selectOne(userBindLambdaQueryWrapper).getMobile();
    }
}
